package com.csh.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.csh.subject.common.enums.CategoryTypeEnum;
import com.csh.subject.common.enums.IsDeletedFlagEnum;
import com.csh.subject.domain.convert.SubjectCategoryConvert;
import com.csh.subject.domain.convert.SubjectLabelConvert;
import com.csh.subject.domain.entity.SubjectLabelBo;
import com.csh.subject.domain.service.SubjectLabelDomainService;
import com.csh.subject.infra.basic.entity.SubjectCategory;
import com.csh.subject.infra.basic.entity.SubjectLabel;
import com.csh.subject.infra.basic.entity.SubjectMapping;
import com.csh.subject.infra.basic.service.SubjectCategoryService;
import com.csh.subject.infra.basic.service.SubjectLabelService;
import com.csh.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public Boolean add(SubjectLabelBo subjectLabelBo) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.add.Bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE
                .convertBoToLabel(subjectLabelBo);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = subjectLabelService.insert(subjectLabel);
        return count>0;
    }

    @Override
    public Boolean update(SubjectLabelBo subjectLabelBo) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.update.Bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE
                .convertBoToLabel(subjectLabelBo);
        int count = subjectLabelService.update(subjectLabel);
        return count>0;
    }

    @Override
    public Boolean delete(SubjectLabelBo subjectLabelBo) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.delete.Bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE
                .convertBoToLabel(subjectLabelBo);
        //删除用的是逻辑删除，调用update方法修改is_deleted字段即可
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count>0;
    }

    @Override
    public List<SubjectLabelBo> queryByCategoryId(SubjectLabelBo subjectLabelBo) {
        //查分类表，如果当前分类是1级分类，则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBo.getCategoryId());
        if(CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()){
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBo.getCategoryId());
            List<SubjectLabel> labelList = subjectLabelService.queryByCondition(subjectLabel);
            List<SubjectLabelBo> labelResultList = SubjectLabelConvert.INSTANCE.convertLabelListToBoList(labelList);
            return labelResultList;
        }

        //查关系表，传入二级分类id，拿到该分类的数据
        Long categoryId = subjectLabelBo.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(mappingList)){
            return Collections.emptyList();
        }
        //拿到分类的数据的标签id列表
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
//        List<Long> labelIdList = mappingList.stream().map(
//                mapping -> mapping.getLabelId()
//        ).collect(Collectors.toList());

        //查标签表，拿到该分类下的标签列表
        List<SubjectLabel> subjectLabelList =subjectLabelService.batchQueryByIds(labelIdList);
        //过滤出未删除的标签列表
        List<SubjectLabel> subjectLabelList1 = subjectLabelList.stream().filter(
                subjectLabel -> IsDeletedFlagEnum.UN_DELETED.getCode() == subjectLabel.getIsDeleted()
        ).collect(Collectors.toList());
        //转subjectLabelBoList
        List<SubjectLabelBo> subjectLabelBoList = subjectLabelList1.stream().map(
                subjectLabel -> {
                    SubjectLabelBo subjectLabelBo1 = new SubjectLabelBo();
                    subjectLabelBo1.setId(subjectLabel.getId());
                    subjectLabelBo1.setLabelName(subjectLabel.getLabelName());
                    subjectLabelBo1.setCategoryId(categoryId);
                    subjectLabelBo1.setSortNum(subjectLabel.getSortNum());
                    return subjectLabelBo1;
                }
        ).collect(Collectors.toList());

//        //foreach写法
//        List<SubjectLabelBO> subjectLabelBoList = new LinkedList<>();
//        subjectLabelList.forEach(subjectLabel -> {
//            SubjectLabelBO bo = new SubjectLabelBO();
//            bo.setId(subjectLabel.getId());
//            bo.setLabelName(subjectLabel.getLabelName());
//            bo.setCategoryId(categoryId);
//            subjectLabelBoList.add(bo);
//        });

        return subjectLabelBoList;
    }
}
